package File;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author liangbaigao
 * @Date 2022/11/1 0:20
 */
public final class FileUtils {


    /**
     * 字节流复制文件内容
     * 适用于所有文件
     * @param sourcePath 文件路径
     * @param targetPath 目标文件
     */
    public static void copyFile(String sourcePath,String targetPath) throws IOException {
        BufferedInputStream bufferedInputStream =
                new BufferedInputStream(Files.newInputStream(Paths.get(sourcePath)));
        BufferedOutputStream bufferedOutputStream =
                new BufferedOutputStream
                        (Files.newOutputStream(Paths.get(targetPath)));
        int b;
        while ((b = bufferedInputStream.read()) != -1){
            bufferedOutputStream.write(b);
            bufferedOutputStream.flush();
        }
    }

    /**
     * 字节流复制文件内容
     * 适用于所有文件
     * @param sourceFile 原文件
     * @param targetFile 目标文件
     * @return targetFile 目标文件
     */
    public static File copyFile(File sourceFile,File targetFile) throws IOException {
        BufferedInputStream bufferedInputStream =
                new BufferedInputStream(Files.newInputStream(sourceFile.toPath()));
        BufferedOutputStream bufferedOutputStream =
                new BufferedOutputStream
                        (Files.newOutputStream(targetFile.toPath()));
        int b;
        while ((b = bufferedInputStream.read()) != -1){
            bufferedOutputStream.write(b);
            bufferedOutputStream.flush();
        }
        return targetFile;
    }

    /**
     * 字符流读取文件
     * 适用于文本文件
     * 查看文件的所有内容并且将内容逐个放进list集合中
     * @param path 文件路径
     * @return list 返回的集合
     */
    public static List readFile(String path) throws IOException {
        BufferedReader bufferedReader =
                new BufferedReader(new FileReader(path));
        String line;
        List<String> list = new ArrayList<>();
        while ((line = bufferedReader.readLine()) != null){
            String[] split = line.split("");
            list.addAll(Arrays.asList(split));
        }
        return list;
    }

    /**
     * 字符流读取文件
     * 适用于文本文件
     * 查看文件的所有内容并且将每一行的内容逐个放进list集合中
     * @param path 文件路径
     * @return list 返回的集合
     */
    public static List<String> readFileLine(String path) throws IOException {
        BufferedReader bufferedReader =
                new BufferedReader(new FileReader(path));
        String line;
        List<String> list = new ArrayList<>();
        while ((line = bufferedReader.readLine()) != null){
            list.add(line);
        }
        return list;
    }

    /**
     * 覆盖字符流写文件
     * @param path 文件路径
     * @param context 写入的内容
     * @param newLine 是否写入后换行
     */
    public static void fileOverWrite(String path,String context,boolean newLine) throws IOException {
        BufferedWriter bufferedWriter =
                new BufferedWriter(new FileWriter(path));
        bufferedWriter.write(context);
        if(newLine) bufferedWriter.newLine();
        bufferedWriter.flush();
        bufferedWriter.close();
    }

    /**
     * 追加字符流写文件
     * @param path 文件路径
     * @param context 写入的内容
     * @param newLine 是否写入后换行
     */
    public static void fileAddWrite(String path,String context,boolean newLine) throws IOException {
        BufferedWriter bufferedWriter =
                new BufferedWriter(new FileWriter(path,true));
        bufferedWriter.write(context);
        if(newLine) bufferedWriter.newLine();
        bufferedWriter.flush();
        bufferedWriter.close();
    }

    /**
     * 文件读取整行后只要部分的数据
     * @param path 文件路径
     * @param start 开始的位置
     * @param end 结束的位置-1
     * @return list 返回的数据集合
     */
    public static List<String> partData(String path, Integer start, Integer end) throws IOException{
        BufferedReader bufferedReader =
                new BufferedReader(new FileReader(path));
        String line;
        List<String> list = new ArrayList<>();
        while ((line = bufferedReader.readLine()) != null){
            list.add(line.substring(start,end));
        }
        return list;
    }
}
